package com.mlh.exception;

import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 缪林辉
 * @date 2024/12/10 11:44
 * @DESCRIPTION
 */
@Component
public class MyHandlerExceptionResolver implements HandlerExceptionResolver ,Ordered {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        if (e instanceof AgeException) {
            // 设置通过exception.jsp页面来展示对异常的处理结果
            ModelAndView modelAndView = new ModelAndView("exception_mes");
            // exception.jsp页面会打印下面这段话
            System.out.println("我是MyHandlerExceptionResolver");
            return modelAndView;
        }
        return null;
    }

    // 如果想优先让我们自定义的异常解析器来处理异常，就实现Ordered接口，然后内容如下写法
    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}
